package com.unity.pruebas3;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.SocketException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;



public class Pruebas3 extends Activity implements OnClickListener{

	private Button btn; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ftp_conn);
		
		btn = (Button)findViewById(R.id.button1);
		
		btn.setOnClickListener(this);
		//amIroot(getApplicationContext());
		
		
	}
	
	private void amIroot(Context context)
	{
		Process p;
		Toast t = Toast.makeText(context, "root", Toast.LENGTH_SHORT);
		try {
		   // Preform su to get root privledges
		   p = Runtime.getRuntime().exec("su"); 

		   // Attempt to write a file to a root-only
//		   DataOutputStream os = new DataOutputStream(p.getOutputStream());
//		   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
//
//		   // Close the terminal
//		   os.writeBytes("exit\n");
//		   os.flush();
//		   
//		   try {
//		      p.waitFor();
//		           if (p.exitValue() != 255) {
//		        	  // TODO Code to run on success
//		              
//		              t.show();
//		              
//		           }
//		           else {
//		        	   // TODO Code to run on unsuccessful
//		        	   t = Toast.makeText(context, "not root", Toast.LENGTH_SHORT);
//		        	   t.show();
//		           }
//		   } catch (InterruptedException e) {
//		      // TODO Code to run in interrupted exception
//			   t = Toast.makeText(context, "not root", Toast.LENGTH_SHORT);
//			   t.show();
//		   }
		}catch (IOException e) {
		   // TODO Code to run in input/output exception
			t = Toast.makeText(context, "not root", Toast.LENGTH_SHORT);
			t.show();
		}
		
		
	}

	public void onClick(View v) {
		
		new Thread(new Runnable() {
	    	public void run() {
	    		try {
					new FtpConn(getApplicationContext());
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		    		
	    	}
	    }).start();
	}
	


}
